<?php
	namespace app\admin\controller;

	use \think\Db;

	class scenic extends Base{
		function show(){
			try{
				if($_POST){
					$page=(int)input('post.page');

					$length=5;

					$start=($page-1)*$length;

					$count=Db::name('scenic')->count('s_id');

					$list=Db::name('scenic')->limit($start,$length)->select();

					if($list){
						return $this->success(['page'=>$page,'length'=>$length,'count'=>$count,'list'=>$list]);
					}

					return $this->fail('数据为空');
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
		function edit_row(){
			try{
				if($_POST){
					$id=(int)input("post.id");

					if(!$id){
						return $this->fail('景点信息不存在');
					}

					$row=Db::name('scenic')->where('s_id',$id)->find();

					if(!$row){
						return $this->fail('景点信息不存在');
					}

					return $this->success(['row'=>$row]);
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
		function edit_btn(){
			try{
				if($_POST){
					$id=(int)input('post.s_id')??0;

					$data=[];

					$data['s_name']=input('post.s_name');

					$data['s_title']=input('post.s_title');

					$data['s_content']=input('post.s_content');

					$data['s_icon']=input('post.s_icon');

					$data['s_ticket']=(int)input('post.s_ticket');

					$data['s_address']=input('post.s_address');

					$data['s_addressId']=$_POST['s_addressId']??[];
					$data['s_addressName']=$_POST['s_addressName']??[];

					$data['s_open']=$_POST['s_open']??[];
					$data['s_week']=$_POST['s_week']??[];
					$data['s_month']=$_POST['s_month']??[];

					if(!$data['s_name']){
						return $this->fail('请输入景点名称');
					}
					if(!$data['s_title']){
						return $this->fail('请输入景点标题');
					}
					if(!$data['s_content']){
						return $this->fail('请输入景点内容');
					}
					if(!$data['s_icon']){
						return $this->fail('请上传景点图片');
					}
					if(!$data['s_address']){
						return $this->fail('请输入详细地址');
					}
					if(!$data['s_addressId']){
						return $this->fail('请选择景点地址');
					}
					if(!$data['s_open'] || !$data['s_week'] || !$data['s_month']){
						return $this->fail('请选择景点开发时间');
					}

					$data['s_addressName'][2]=str_replace("县","区",$data['s_addressName'][2]);

					$data['s_qu']=$data['s_addressName'][2];

					$old_icon=str_replace("//".$_SERVER['HTTP_HOST'],'',$data['s_icon']);
					$new_icon=str_replace("temp","upload",$old_icon);


					if($id){
						if(!is_file('.'.$new_icon)){
							if(copy('.'.$old_icon,'.'.$new_icon)){
								unlink('.'.$old_icon);

								$data['s_icon']='//'.$_SERVER['HTTP_HOST'].$new_icon;
							}
						}

						$res=Db::name('scenic')->where('s_id',$id)->update($data);
					}else{
						if(copy('.'.$old_icon,'.'.$new_icon)){
							unlink('.'.$old_icon);

							$data['s_icon']='//'.$_SERVER['HTTP_HOST'].$new_icon;
						}

						$row=Db::name('primary')->where('type','scenic')->find();

						if(!$row){
							$res=Db::name('primary')->insert(['type'=>'scenic','id'=>1]);

							$id=1;
						}else{
							$id=$row['id']+1;

							$res=Db::name('primary')->where('type','scenic')->update(['id'=>$id]);
						}

						$data['s_id']=$id;


						$res=Db::name('scenic')->insert($data);
					}

					return $this->success();
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
		function dele_btn(){
			try{
				if($_POST){
					$id=(int)input('post.id');

					if(!$id){
						return $this->fail('当前景点信息不存在');
					}

					$row=Db::name('scenic')->where('s_id',$id)->find();

					if(!$row){
						return $this->fail('当前景点信息不存在');
					}

					$icon=$row['s_icon'];

					$icon=str_replace('//'.$_SERVER['HTTP_HOST'],'',$icon);

					if(is_file('.'.$icon)){
						unlink('.'.$icon);
					}

					$res=Db::name("scenic")->where('s_id',$id)->delete();

					if($res){
						return $this->success();
					}

					return $this->fail('删除失败');
				}
			}catch(\Exception $e){
				return $this->err($e);
			}
		}
	}
?>